<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<script type="text/javascript" charset="UTF-8">
/**
 if (!window.//console || !//console.firebug){
    var names = ["log", "debug", "info", "warn", "error", "assert", "dir", "dirxml",
    "group", "groupEnd", "time", "timeEnd", "count", "trace", "profile", "profileEnd"];
    window.//console = {};
    for (var i = 0; i < names.length; ++i)
        window.//console[names[i]] = function() {};
   }
 */

	var moduls_detContent_propertyGrid;
	$(function() {
		moduls_detContent_propertyGrid = $('#moduls_detContent_propertyGrid').propertygrid({
			url : '${pageContext.request.contextPath}/detContentAction!query.action',
			showGroup : true,
			scrollbarSize : 0,
			fit : true,
			groupField : 'posName',
			fitColumns : false,
			border : false,
			toolbar : [ {
				text : '增加',
				iconCls : 'icon-add',
				handler : function() {
					moduls_detContent_addFun();
				}
			}, '-', {
				text : '批量删除',
				iconCls : 'icon-remove',
				handler : function() {
					moduls_detContent_removeFun();
				}
			}, '-', {
				text : '展开',
				iconCls : 'icon-redo',
				handler : function() {
					var node = $('#moduls_detContent_propertyGrid').propertygrid('getSelected');
					if (node) {
						$('#moduls_detContent_propertyGrid').propertygrid('expandGroup', node.cid);
					} else {
						$('#moduls_detContent_propertyGrid').propertygrid('expandGroup');
					}
				}
			}, '-', {
				text : '折叠',
				iconCls : 'icon-undo',
				handler : function() {
					var node = $('#moduls_detContent_propertyGrid').propertygrid('getSelected');
					if (node) {
						$('#moduls_detContent_propertyGrid').propertygrid('collapseGroup', node.cid);
					} else {
						$('#moduls_detContent_propertyGrid').propertygrid('collapseGroup');
					}
				}
			}, '-', {
				text : '刷新',
				iconCls : 'icon-reload',
				handler : function() {
					$('#moduls_detContent_propertyGrid').propertygrid('reload');
				}
			} ],
			columns : [ [ {
				title : '测评项目ID',
				field : 'contentId',
				width : 50,
				hidden : true
			}, {
				title : '测评项目',
				field : 'projectName',
				width : 100
			}, {
				title : '测评内容',
				field : 'detContent',
				width : 450
			}, {
				title : '测评选项',
				field : 'detproOption',
				width : 170,
				formatter : function(value, row, index) {
					var optStr = '';
					try {
						var option = $.parseJSON(value);
						for(var i = 0; option && i < option.length; i++) {
							if (optStr.length == 0) {
								optStr += option[i].textField + ':' + option[i].valueField;
	
							} else {
								optStr += ', ' + option[i].textField + ':' + option[i].valueField;
							}
						}
					} catch (e) {
					}
					
					return optStr;
				}
			}, {
				field : 'action',
				title : '动作',
				width : 100,
				formatter : function(value, row, index) {
					if (row.id == '0') {
						return '系统角色';
					} else {
						return formatString('<img onclick="moduls_detContent_editFun(\'{0}\');" src="{1}"/>&nbsp;&nbsp;&nbsp;<img onclick="moduls_detContent_deleteFun(\'{2}\');" src="{3}"/>', row.contentId, 'jslib/jquery-easyui-1.3.1/themes/icons/pencil.png', row.contentId, 'jslib/jquery-easyui-1.3.1/themes/icons/cancel.png');
					}
				}
			} ] ]
		});
	});
	
	function dealData(str) {
		var detOptions = [];
		var option = str.replace(/，/g,',').replace(/：/g,':').replace(/ /g,'').split(',');
		var detOption;
		for(var i = 0; i < option.length; i++) {
			var opt = option[i].split(':');
			detOption = new Object();
			detOption.value = 0;
			detOption.textField = opt[0];
			detOption.valueField = opt[1];
			detOptions.push(detOption);
		}
		return detOptions;
	}

	var moduls_detContent_addFun_PositionComboBoxData;
	function moduls_detContent_addFun() {
		moduls_detContent_propertyGrid.propertygrid('uncheckAll').propertygrid('unselectAll').propertygrid('clearSelections');
		$('<div/>').dialog({
			href : '${pageContext.request.contextPath}/moduls/fun/addDetContent.jsp',
			width : 400,
			height : 220,
			modal : true,
			title : '添加测评内容',
			buttons : [ {
				text : '保存',
				iconCls : 'icon-save',
				handler : function() {
					var d = $(this).closest('.window-body');
					
					var str = $('#moduls_fun_addDetContent_addForm input[name=detproOption]').val();
					var detOptions = dealData(str);
					var posType = $('#moduls_fun_addDetContent_addForm input[name=posType]').val();
					
					$.ajax({
						type : 'POST',
						url : '${pageContext.request.contextPath}/detContentAction!add.action',
						data : {
							posId : $('#moduls_fun_addDetContent_addForm input[name=posId]').val(),
							projectName : $('#moduls_fun_addDetContent_addForm input[name=projectName]').val(),
							detContent : $('#moduls_fun_addDetContent_addForm input[name=detContent]').val(),
							detproOption : posType == 1 ? "" : $.toJSON(detOptions)
						},
						cache : false,
						dataType : 'JSON',
						success : function(r) {
							if (r.success) {
								moduls_detContent_propertyGrid.propertygrid('load');
								d.dialog('destroy');
							}
							$.messager.show({
								title : '提示',
								msg : r.msg
							});
						}
					});
					
				}
			}, {
				text : '取消',
				iconCls : 'icon-remove',
				handler : function() {
					$(this).closest('.window-body').dialog('destroy');
				}
			} ],
			onClose : function() {
				$(this).dialog('destroy');
			}
		});
	}

	function moduls_detContent_editFun(id) {
		moduls_detContent_propertyGrid.propertygrid('uncheckAll').propertygrid('unselectAll').propertygrid('clearSelections');
		var index = moduls_detContent_propertyGrid.propertygrid('getRowIndex', id);
		moduls_detContent_propertyGrid.propertygrid('selectRow', index);
		$('<div/>').dialog({
			href : '${pageContext.request.contextPath}/moduls/fun/editDetContent.jsp',
			width : 400,
			height : 220,
			modal : true,
			title : '编辑测评内容',
			buttons : [ {
				text : '保存',
				iconCls : 'icon-save',
				handler : function() {
					var d = $(this).closest('.window-body');
					
					var str = $('#moduls_fun_editDetContent_editForm input[name=detproOption]').val();
					var detOptions = dealData(str);
					var posType = $('#moduls_fun_editDetContent_editForm input[name=posType]').val();
					
					$.ajax({
						type : 'POST',
						url : '${pageContext.request.contextPath}/detContentAction!modify.action',
						data : {
							contentId : $('#moduls_fun_editDetContent_editForm input[name=contentId]').val(),
							posId : $('#moduls_fun_editDetContent_editForm input[name=posId]').val(),
							projectName : $('#moduls_fun_editDetContent_editForm input[name=projectName]').val(),
							detContent : $('#moduls_fun_editDetContent_editForm input[name=detContent]').val(),
							detproOption : posType == 1 ? "" : $.toJSON(detOptions)
						},
						cache : false,
						dataType : 'JSON',
						success : function(r) {
							if (r.success) {
								moduls_detContent_propertyGrid.propertygrid('load');
								d.dialog('destroy');
							}
							$.messager.show({
								title : '提示',
								msg : r.msg
							});
						}
					});
				}
			}, {
				text : '取消',
				iconCls : 'icon-remove',
				handler : function() {
					$(this).closest('.window-body').dialog('destroy');
				}
			} ],
			onClose : function() {
				$(this).dialog('destroy');
			},
			onLoad : function() {
				var rows = moduls_detContent_propertyGrid.propertygrid('getSelected');
				changeDetContentPosType(rows.posType);
				$('#moduls_fun_editDetContent_editForm').form('load', rows);
				
				var optStr = '';
				try {
					var option = $.parseJSON(rows.detproOption);
					for(var i = 0; option && i < option.length; i++) {
						if (optStr.length == 0) {
							optStr += option[i].textField + ':' + option[i].valueField;
							
						} else {
							optStr += ', ' + option[i].textField + ':' + option[i].valueField;
						}
					}
				} catch (e) {
				}
				$('#moduls_fun_editDetContent_editForm input[name=detproOption]').val(optStr);
			}
		});
	}

	function moduls_detContent_deleteFun(node, text) {
		if (node) {
			$.messager.confirm('询问', '您确定要删除选中项目？', function(b) {
				if (b) {
					$.ajax({
						url : '${pageContext.request.contextPath}/detContentAction!delete.action',
						data : {
							ids : node
						},
						cache : false,
						dataType : 'JSON',
						success : function(r) {
							if (r.success) {
								moduls_detContent_propertyGrid.propertygrid('load');
							}
							$.messager.show({
								msg : r.msg,
								title : '提示'
							});
						}
					});
				}
			});
		}
	}
</script>
<table id="moduls_detContent_propertyGrid"></table>
